<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title></title>
	</head>
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<link href="css/jquery.fancybox.css?v=2.1.2" rel="stylesheet" type="text/css" />
	<link href="css/commentStyle.css" rel="stylesheet" type="text/css" />

	<body>

		<!--[if lte IE 8]>
  	<div style=' clear: both; height: 59px; padding:0 0 0 15px; position: relative;margin:0 auto;'>
	    <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
	      <img src="http://7u2nvr.com1.z0.glb.clouddn.com/picouterie.jpg" border="0" height="42" width="820"
	           alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today or use other browser ,like chrome firefox safari."
	           style='margin-left:auto;margin-right:auto;display: block;'/>
	    </a>
  	</div>
	<![endif]-->
		<div class="container">

			<!--headeer begin-->
			<header class="header"></header>
			<!--headeer end-->

			<!--main begin-->

			<main id="main" class="main">
				<div class="main-inner">
					<div id="content" class="content">

						<div id="posts" class="posts-expand">

							<article class="post post-type-normal " itemscope="" itemtype="http://schema.org/Article">

								<header class="post-header">

									<h1 class="post-title" itemprop="name headline">
            
            
              
                让普通 Java 类自动感知 Activity Lifecycle
              
          </h1>

									<div class="post-meta">

										<i class="fa fa-bicycle"></i>

										<span class="post-time">发表于
								            <time>
								              2017-11-09
								            </time>
								        </span>

										<span class="post-category">
              								&nbsp; | &nbsp; 分类于
              							<span itemprop="about" itemscope="" itemtype="https://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
										</a>
										</span>

										</span>

										<span id="busuanzi_container_page_pv" style="display: inline;">
              &nbsp; | &nbsp;
              本文总阅读量<span id="busuanzi_value_page_pv">27</span>次
										</span>

									</div>
								</header>

								<div class="post-body">

									<span><h2 id="背景"><a href="# 背景" class="headerlink" title="背景"></a>背景 </h2><p> 在 Android 开发中，我们都很熟悉 Activity 的 Lifecycle，并且会在特定的 Lifecycle 下执行特定的操作。当然，我们清楚 Lifecycle 本身是带有 Android 特质的，那尝试设想下，如果 <code> 普通的 Java Class 也能自动感知
									 Lifecycle 呢</code>？咋一听这个想法似乎背后意义不大，但在实际探索中，我们发现这个特性能为我们达成一些之前未考虑到或者不易实现的优化。</p>
									<p>本文分享下我们基于这个思想所开发的框架：<code>AutoLifecycle</code> 及其带来的一些有意思的实践。</p>
									<a id="more"></a>
									<ul>
									<li>优化一：当 Activity 进入 onDestroy 时，自动取消网络请求返回</li>
									<li>优化二：自动将网络请求时机提前到 View 渲染之前，提高页面打开速度</li>
									<li>优化三：MVP 改进，让 Presenter 和 View 自动 bind/unBind</li>
									</ul>
									<p>注：下文提到的 <code>Lifecycle-Aware</code> 就是这里指代的 <code> 让普通 Java Class 自动获取 Lifecycle</code>。</p>
									<h2 id="实践及优化"><a href="# 实践及优化" class="headerlink" title="实践及优化"></a>实践及优化 </h2><h3 id="优化一：当 Activity 进入 onDestroy 时，自动取消网络请求返回"><a href="# 优化一：当 Activity 进入 onDestroy 时，自动取消网络请求返回" class="headerlink" title="优化一：当 Activity 进入 onDestroy 时，自动取消网络请求返回"></a> 优化一：当 Activity 进入 onDestroy 时，自动取消网络请求返回 </h3><p> 在网络请求时，相信大家都有一个经验：在每个网络结果回来时，我们做的第一件事不是显示数据，而是写个 if-else 判断 Activity 还在不在。
									<br></p>
								<p></p>
								<p>由于网络请求都是异步的，所以不得不做这样的判断，来防止不可预测的空指针问题或内存泄漏问题。</p>
								<p>既然你总是担心 <code>Activity</code> 还在不在，那么如果我们通过 <code>Lifecycle-Aware 让每个网络请求能自动感知 Activity 的 onDestroy 事件</code>，<br> 并在 <code>onDestroy</code> 时，自动把网络请求结果 <code> 取消掉不再返回</code>，那就能够消除这个担忧了。<br></p>
								
								<h2 id="参考"><a href="# 参考" class="headerlink" title="参考"></a>参考</h2>
								<p>
									<a href="https://developer.android.com/topic/libraries/architecture/lifecycle.html" target="_blank" rel="external">https://developer.android.com/topic/libraries/architecture/lifecycle.html</a><br>
								</p>
								</span>

								</div>

								<footer class="post-footer">

								<div class="post-tags">

								<a href="/tags/Android/" rel="tag">#Android</a>

								<a href="/tags/Java/" rel="tag">#Java</a>

								<a href="/tags/Lifecycle/" rel="tag">#Lifecycle</a>

							</div>

								<div class="post-nav">
									<h3>相关文章：</h3>
									<ul class="related-posts">
										<li>
											<a href="/2016/08/30/谈谈移动应用的安全性实践/">谈谈移动应用的安全性实践</a>
										</li>
										<li>
											<a href="/2016/03/15/30条Android开发建议/">30 条 Android 开发建议</a>
										</li>
									</ul>
								</div>

								<div class="post-nav">
									<div class="post-nav-next post-nav-item">
	
										<a href="/2017/11/06/alibaba-qa/" rel="next" title="阿里求职付费群问答">
											<i class="fa fa-chevron-left"></i> 阿里求职付费群问答
										</a>
	
									</div>
	
									<div class="post-nav-prev post-nav-item">
	
									</div>
								</div>


								<div class="comments">
									<div class="post-header">
										<h1 class="h1 post-title">评论<a class="comment-expand">收起</a></h1> 
									</div>

									<div class="commentAll">
										<!--评论区域 begin-->
										<div class="clearfix">
											<input class="comment-name" type="text" placeholder="Your name" />
											<textarea class="comment-content comment-input" placeholder="Please enter a comment&hellip;" onkeyup="keyUP(this)"></textarea>
											<a href="javascript:;" class="plBtn">评论</a>
										</div>
										<!--评论区域 end-->

										<!--回复区域 begin-->
										<div class="commentShow">
											<!--回复回复列表-->

											<div class="comment-show-con clearfix">

											<div class="comment-show-con-img ">
													<img src="img/author/1.jpg" width="48px" height="48px" alt="">
											</div>

												<div class="comment-show-con-list clearfix">
													<!--评论内容-->
													<div class="pl-text clearfix">
														<a href="#" class="comment-size-name">1：</a>
														<span class="my-pl-con"> what a beautifal firework</span>
													</div>
													<!--评论日期-->
													<div class="date-dz">
														<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
														<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
													</div>
													<div class="hf-list-con">

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

													</div>
													<span class="hf-expand">展开<span class="hf-count"></span>条回复</span>
												</div>
											</div>

											<div class="comment-show-con clearfix">
												<div class="comment-show-con-img "><img src="img/author/1.jpg" width="48px" height="48px" alt=""></div>
												<div class="comment-show-con-list  clearfix">
													<!--评论内容-->
													<div class="pl-text clearfix">
														<a href="#" class="comment-size-name">2：</a>
														<span class="my-pl-con"> what a beautifal firework</span>
													</div>
													<!--评论日期-->
													<div class="date-dz">
														<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
														<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
													</div>
													<div class="hf-list-con">

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

													</div>
													<span class="hf-expand">展开<span class="hf-count"></span>条回复</span>
												</div>
											</div>

											<div class="comment-show-con clearfix">
												<div class="comment-show-con-img "><img src="img/author/1.jpg" width="48px" height="48px" alt=""></div>
												<div class="comment-show-con-list  clearfix">
													<!--评论内容-->
													<div class="pl-text clearfix">
														<a href="#" class="comment-size-name">3：</a>
														<span class="my-pl-con"> what a beautifal firework</span>
													</div>
													<!--评论日期-->
													<div class="date-dz">
														<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
														<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
													</div>
													<div class="hf-list-con">

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

													</div>
													<span class="hf-expand">展开<span class="hf-count"></span>条回复</span>
												</div>
											</div>

											<div class="comment-show-con clearfix">
												<div class="comment-show-con-img "><img src="img/author/1.jpg" width="48px" height="48px" alt=""></div>
												<div class="comment-show-con-list  clearfix">
													<!--评论内容-->
													<div class="pl-text clearfix">
														<a href="#" class="comment-size-name">4：</a>
														<span class="my-pl-con"> what a beautifal firework</span>
													</div>
													<!--评论日期-->
													<div class="date-dz">
														<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
														<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
													</div>
													<div class="hf-list-con">

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

													</div>
													<span class="hf-expand">展开<span class="hf-count"></span>条回复</span>
												</div>
											</div>

											<div class="comment-show-con clearfix">
												<div class="comment-show-con-img "><img src="img/author/1.jpg" width="48px" height="48px" alt=""></div>
												<div class="comment-show-con-list  clearfix">
													<!--评论内容-->
													<div class="pl-text clearfix">
														<a href="#" class="comment-size-name">5：</a>
														<span class="my-pl-con"> what a beautifal firework</span>
													</div>
													<!--评论日期-->
													<div class="date-dz">
														<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
														<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
													</div>
													<div class="hf-list-con">
														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

														<div class="all-pl-con">
															<div class="pl-text hfpl-text clearfix">
																<a href="#" class="comment-size-name">test：</a>
																<span class="my-pl-con">回复<a href="#" class="atName">legolas：</a>test test test test test</span>
															</div>
															<div class="date-dz">
																<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
																<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
															</div>
														</div>

													</div>
													<span class="hf-expand">展开<span class="hf-count"></span>条回复</span>
												</div>
											</div>

											<div class="comment-show-con clearfix">
												<div class="comment-show-con-img "><img src="img/author/1.jpg" width="48px" height="48px" alt=""></div>
												<div class="comment-show-con-list  clearfix">
													<!--评论内容-->
													<div class="pl-text clearfix">
														<a href="#" class="comment-size-name">6：</a>
														<span class="my-pl-con"> what a beautifal firework</span>
													</div>
													<!--评论日期-->
													<div class="date-dz">
														<span class="date-dz-left  comment-time">2017-5-2 11:11:39</span>
														<a href="javascript:;" class="date-dz-pl pl-hf hf-con-block ">回复</a>
													</div>

													<div class="hf-list-con"></div>

													<span class="hf-expand">展开<span class="hf-count"></span>条回复</span>
												</div>
											</div>

										</div>
										<!--回复区域 end-->

										<div class="commentFooter"><span class="fa fa-angle-double-down" />加载更多</div>
									</div>

								</div>

								</footer>

							</article>

						</div>

					</div>

				</div>

			</main>

			<!--main end-->

			<aside class="sidebar"></aside>

			<!--footer begin-->
			<footer class="footer"></footer>
			<!--footer end-->

		</div>

		<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>
		<script src="js/jquery.fancybox.js?v=2.1.3" type="text/javascript"></script>
		<script src="js/comment.js" type="text/javascript"></script>
		<script type="text/javascript">
			$(function() {

				$.get("public/header.html", function(data) {
					$(".header").html(data);
				});
				$.get("public/sidebar.html", function(data) {
					$(".sidebar").html(data);
				});
				$.get("public/footer.html", function(data) {
					$(".footer").html(data);
				});

				$('.fancybox').fancybox();

			});
		</script>
	</body>

</html>